<template>
  <BasicTable ref="basicTable" @register="registerTable" :rowSelection="rowSelection">
    <template #action="{ record }">
      <!--操作栏-->
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--弹窗区域-->
  <todoTaskModel @register="registerModal" @success="handleSuccess"></todoTaskModel>
</template>
<script lang="ts" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { columns } from './todoTask.data';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useModal } from '/@/components/Modal';
  import { ref } from 'vue';
  import todoTaskModel from './components/TodoTaskModel.vue';
  import { list, queryByUserId } from './todoTask.api';
  import { searchFormSchema } from './todoTask.data';
  import { onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t: inventory } = useI18n('asset.inventoryManagement.fields');
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //自己请求数据
  // const inventoryTask = ref([]);
  onMounted(async () => {
    // 进行异步请求数据
    // await list({ inventoryState: '0', pageNo: 1, pageSize: 10 }).then((res) => {
    //   console.log(res, 'mmmmmmmmmmmmm');
    //   inventoryTask.value = res.records;
    // });
    // console.log(data, 'mmmmmmmmm');
    // 处理数据
    // inventoryTask.value = data;
  });
  //table配置项
  const { tableContext } = useListPage({
    tableProps: {
      // dataSource: inventoryTask,
      api: queryByUserId,
      columns,
      canResize: false,
      // beforeFetch: (params) => {
      //   return { ...params, userId: userStore.userInfo?.username };
      // },

      beforeFetch: (params) => {
        return Object.assign(params, { userId: userStore.userInfo?.username });
      },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
  });

  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
  const basicTable = ref<InstanceType<typeof BasicTable>>();
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
      hideFooter: true,
    });
  }
  //盘点操作
  function inventoryOperation(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: inventory('view'),
        onClick: handleDetail.bind(null, record),
      },
      {
        label: inventory('inventory'),
        onClick: inventoryOperation.bind(null, record),
      },
    ];
  }
</script>
